/**
 * @filename:MoldChainController Jan 14, 2025
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd.
 * All right reserved.
 */
package com.wonder.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.MoldInspect;
import com.wonder.app.request.MoldInspectRequest;
import com.wonder.app.service.MoldInspectService;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.StringUtils;
import com.wonder.framework.authorization.CheckAdminToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * <p>说明： 仓位列表API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Jan 14, 2025
 *
 */
@Api(tags = "模具链列表")
@RestController
@RequestMapping("/app/MoldInspect")
public class MoldInspectController {

@Autowired
private MoldInspectService moldInspectService;


    @Autowired
    private SysAdminService adminService;

    @Autowired
    private CheckAdminToken checkAdminToken;

    @ApiOperation(value = "模具链列表")
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public Map<String,Object> index(@RequestBody MoldInspectRequest moldInspectRequest) {
        Page<MoldInspect> page = new Page<MoldInspect>(moldInspectRequest.getPage(), moldInspectRequest.getLimit());
        IPage<MoldInspect> list = moldInspectService.selectList(moldInspectRequest, page);

        Map<String,Object> retMap=new HashMap<>();
        retMap.put("data",CommonPage.restPage(list));


        return AjaxMap.success(retMap);
    }

    /**
     * @explain 修改排序开关
     * @param   entity
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/updateExt")
    @ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
    public AjaxResult<MoldInspect> updateExt(@RequestBody MoldInspect entity){
        if (null!=entity) {
            boolean rsg =  moldInspectService.updateById(entity);
            if (rsg) {
                return AjaxResult.success("system.updateSuccess");
            }else {
                return AjaxResult.failed("system.updateFailed");
            }
        }else {
            return AjaxResult.failed("system.parameterError");
        }
    }

    /**
     * @explain 添加
     * @param   entity
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加", notes = "作者：Wonder")
    public AjaxResult<?> add(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        String userName = user.getName();
        Boolean res = moldInspectService.createMoldInspect(userName,map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }
    /**
     * @explain 修改
     * @param   entity
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "作者：Wonder")
    public AjaxResult<MoldInspect> update(@RequestBody MoldInspect entity){
        if (null!=entity) {
            String validate=moldInspectService.validate(entity,"update");
            if(StringUtils.hasText(validate))
                return AjaxResult.failed(validate);
            boolean rsg = moldInspectService.updateById(entity);
            if (rsg) {
                return AjaxResult.success("system.updateSuccess");
            }else {
                return AjaxResult.failed("system.updateFailed");
            }
        }else {
            return AjaxResult.failed("system.parameterError");
        }
    }
    /**
     * @explain 查看对象明细
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "查询prdfeed明细")
    @ResponseBody
    @PostMapping(value = "/moldloadd")

    public AjaxResult<Map<String,Object>> getmoldloDetail(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = moldInspectService.getmoldloDetail(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }
    /**
     * @explain 删除
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除", notes = "作者：Wonder")
    public AjaxResult<MoldInspect> delete(@RequestBody Map<String,Object> map){
        if(map==null)return AjaxResult.failed("system.parameterError");
        String ids=map.get("id").toString();
        ids=StringUtils.remove(ids, '[');
        ids=StringUtils.remove(ids, ']');
        String[] deleteIds=StringUtils.split(ids, ',');
        List<Long> idList=new ArrayList<>();
        for(int i=0;i<deleteIds.length;i++)
        {
            idList.add( Long.parseLong(deleteIds[i].trim()));//整数数组
        }

        boolean rsg = moldInspectService.removeBatchByIds(idList);
        if (rsg) {
            return AjaxResult.success("system.deleteSuccess");
        }else {
            return AjaxResult.failed("system.deleteFailed");
        }

    }

    @ApiOperation(value = "模具检修单列表(手机端)")
    @RequestMapping(value = "/get_mold_inspect_list", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> getMoldMnspectList(@RequestBody Map<String,Object> map) throws Exception{


        Map<String,Object> data = moldInspectService.getMoldInspectList(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }

    /**
     * @explain 查询模具检查单明细(手机端)
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "查询模具检查单明细(手机端领料详情)")
    @ResponseBody
    @PostMapping(value = "/get_mold_inspect_detail")

    public AjaxResult<Map<String,Object>> getMoldInspectDetail(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = moldInspectService.getMoldInspectDetail(map,user_id);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }


    /**
     * 检查单领用模具
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "(检查单领用模具)")
    @ResponseBody
    @PostMapping(value = "/mold_inspect_pick_mold")

    public AjaxResult<?> moldInspectPickmold(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Boolean res = moldInspectService.moldInspectPickmold(userName,map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }

    }

    /**
     * 审核模具检查单
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "(审核模具检查单)")
    @ResponseBody
    @PostMapping(value = "/audit_mold_inspect")

    public AjaxResult<?> auditMoldInspect(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Boolean res = moldInspectService.auditMoldInspect(user_id,userName,map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }

    }
}
